package pong.game.open_gl;

import pong.mathematic.Engine;
import pong.mathematic.Vector2D;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.SurfaceHolder;

public class MyGLSurfaceView extends GLSurfaceView {

	private static final String TAG = "MyGLSurfaceView";
	private static final int MOVE_BARIER = 30;
	private static boolean wasDestroyed = false;
	private static boolean previousRenderSleppingState = false;

	private final ScaleGestureDetector scaleGestureDetector;

	private float downPosY;
	private float downPosX;
	private boolean moveFlag = false;
	private final Vector2D firstTouch = new Vector2D();

	private boolean isMultiTouch = false;

	public MyGLSurfaceView(Context context, AttributeSet attrs) {
		super(context, attrs);
		scaleGestureDetector = new ScaleGestureDetector(context, new MyScaleGestureListener());
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		scaleGestureDetector.onTouchEvent(event);

		int action = event.getAction() & MotionEvent.ACTION_MASK;
		// String message = "";

		if (event.getPointerCount() == 1) {
			// SingleTouch
			switch (action) {
				case MotionEvent.ACTION_DOWN:
					if (!isMultiTouch) {
						downPosX = event.getX();
						downPosY = event.getY();

						firstTouch.setX(downPosX);
						firstTouch.setY(downPosY);
						Engine.getInstance().touchDown(downPosX, downPosY, this.getWidth(), this.getHeight());
						// message = "touch Down";
					}
					break;
				case MotionEvent.ACTION_MOVE:
					if (!isMultiTouch) {
						int posCount = event.getHistorySize();
						if (posCount > 0) {
							float oldX = event.getHistoricalX(posCount - 1);
							float oldY = event.getHistoricalY(posCount - 1);
							downPosX = event.getX();
							downPosY = event.getY();

							float speedX = (downPosX - oldX) / this.getWidth();
							float speedY = -(downPosY - oldY) / this.getHeight();

							Engine.getInstance().touchMove(downPosX, downPosY, speedX, speedY, this.getWidth(),
									this.getHeight());
							// message = "move_touch history";
						} else {
							float speedX = (event.getX() - downPosX) / this.getWidth();
							float speedY = -(event.getY() - downPosY) / this.getHeight();

							downPosX = event.getX();
							downPosY = event.getY();

							// Log.v(TAG, "firstTouch.getX()=" +
							// firstTouch.getX());
							// Log.v(TAG, "firstTouch.getY()=" +
							// firstTouch.getY());

							if (!moveFlag && Math.abs(downPosX - firstTouch.getX()) < MOVE_BARIER
									&& Math.abs(downPosY - firstTouch.getY()) < MOVE_BARIER) {
								break;
							}

							moveFlag = true;

							Engine.getInstance().touchMove(downPosX, downPosY, speedX, speedY, this.getWidth(),
									this.getHeight());
							// message = "move_touch";
						}
					}
					break;

				case MotionEvent.ACTION_UP:
					moveFlag = false;
					Engine.getInstance().touchUp(isMultiTouch);

					if (isMultiTouch && event.getPointerCount() == 1) {
						isMultiTouch = false;

					}
					// message = "touch Up";
					break;
				default:
					// message = "Skipped touch: " + action;
			}
		} else
			isMultiTouch = true;
		// Log.v("Action", message);
		return true;
	}

	@Override
	public void onPause() {
		Log.d(TAG, "GLSurface paused (not worked!)");
		// Data.getInstance().fadeIn(1);
		setRendererSleep(true);
		// ((HomeMenuActivity) getContext()).showTempBackground(0);
		super.onPause();
	}

	private void setRendererSleep(boolean slepping) {
		try {
			// ((HomeMenuActivity)
			// getContext()).getDiamondCanvasView().setRendererSleep(slepping);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean isSleep() {
		return false;
		// return ((SocialPoing)
		// getContext()).getDiamondCanvasView().isRendererSleeping();
	}

	@Override
	public void onResume() {

		Log.d(TAG, "GLSurface is resumed (not worked!)");

		// Data.getInstance().suspendBitmapQueue(true);

		// CanvasData.getInstance().resumeBitmapQueue();

		Engine.getInstance().updateTime();

		setRendererSleep(previousRenderSleppingState);

		super.onResume();

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		super.surfaceDestroyed(holder);
		// CanvasData.getInstance().resetTexturesID(false);
		// CanvasData.getInstance().suspendBitmapQueue(true);
		Log.d(TAG, "GLSurface is destroyed");
		wasDestroyed = true;

	}

	public class MyScaleGestureListener implements OnScaleGestureListener {

		public boolean onScale(ScaleGestureDetector detector) {
			float deltaZ = (detector.getScaleFactor() - 1) * -50;
			Engine.getInstance().cameraScale(deltaZ);
			return true;
		}

		public boolean onScaleBegin(ScaleGestureDetector detector) {
			isMultiTouch = true;
			return true;
		}

		public void onScaleEnd(ScaleGestureDetector detector) {
			// TODO Auto-generated method stub
			isMultiTouch = false;
		}
	}

}
